package com.star.uc.infras.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截token，并校验token
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/10/18 15:51
 */
//@Component
public class CustomInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        // 获取过期时间配置
        long tokenTimeout = StpUtil.getTokenTimeout();

        /**
         * token没过期，过期时间不是-1(永久)的时候，每次请求都刷新过期时间,在1800s(半个小时)不操作就过期了。
         */
        if (tokenTimeout != -1) {
            // 用于token续期(1800s)
            StpUtil.renewTimeout(1800);
            StpUtil.updateLastActiveToNow();
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}
